/**
 * @file detail.tsx
 * @description 待配送详情
 */
 import React,{ useState } from 'react';
 import { Vibration} from "react-native";
 import { Box, useToast, Pressable, Avatar, HStack, VStack, Text, Spacer, Center, Button,ScrollView,} from 'native-base';
 import HeaderScreen from '../../components/header';
 import * as request from "../../request/common";

 import Sound from 'react-native-sound';
 import { useMount } from "ahooks";
 
 let successSound: any; //成功声音
 let failSound: any; // 失败声音
 let pageGoBack = false; // 返回
 const ScanDetailScreen: React.FC<ComponentProp> = ({ navigation,route }) => {
   /******变量******/
   const routeParams: any = route?.params;
   const [detailInfo, setDetailInfo] = useState<any>(); // 详情信息
   const toast = useToast();
 

 
   /******生命周期******/
  useMount(async () => {
    if(routeParams?.scanCode?.trim()?.length > 0){
      getDetails(routeParams?.scanCode);
    }
    console.log('初始化完成！！！！');
    // 初始化声音
    Sound.setCategory('Playback'); // 设置音频播放类型
    // 创建音频对象
    successSound = new Sound('successSound.mp3', Sound.MAIN_BUNDLE, error => {
      if (error) {
        console.log('声音加载失败', error);
        return;
      }
      console.log('声音加载成功');
  
    });
     failSound = new Sound('failSound.mp3', Sound.MAIN_BUNDLE, error => {
      if (error) {
        console.log('声音加载失败', error);
        return;
      }
      console.log('声音加载成功');
    });
  });
 
   /******方法******/
 
   /**
    * 根据pk码获取详细信息
    */
   const getDetails = (scanCode: string) => {
     request.getPackagesInfo(scanCode.trim()).then((res: any) => {
       if (res?.code == 40020) {
         setDetailInfo(res?.data);
         if (
           routeParams?.productionInfo?.factory_code_id ==
           res?.data?.factory_code_id
         ) {
          updateModel(res?.data);
         } else {
          Vibration.vibrate(500);
           // 不是本生产单的包装！
           toast.show({
            placement: "top",
             description: '不是本生产单的包装！',
           });
            // 播放声音
            failSound.play((success: any) => {
              if (success) {
                console.log('声音播放完成');
              } else {
                console.log('声音播放失败');
              }
            });
           setTimeout(() => {
             // 自动返回
             if(!pageGoBack){
               navigation?.goBack();
             }
           }, 3 * 1000);
         }
       }
     });
   };

   /**
    * 入库
    */
   const updateModel = (detailInfo: any) => {
     request
       .updateInOrOutPicking(detailInfo?.factory_code_id, detailInfo.package_index)
       .then((res: any) => {
         if (res?.code == 40020) {
           // 播放声音
           successSound.play((success: any) => {
             if (success) {
               console.log('声音播放完成');
             } else {
               console.log('声音播放失败');
             }
           });
           setTimeout(() => { // 自动返回
            if(!pageGoBack){
              navigation?.goBack();
            }
           }, 2 * 1000);
         }
       });
   };

   /**
    * 手动查询
    * @param search pk码
    */
   const handleSearch = (search?: string) => {
    getDetails(search|| '');
  };
 
 
   return (
    <Pressable onPress={()=>{
      // 点击返回
      pageGoBack = true;
      navigation?.goBack();
    }}>
      <Box bg="gray.100" borderRadius="md" w="100%" h="100%">
            <HeaderScreen
              navigation={navigation}
              search={routeParams?.scanCode =='' ? true : false}
              searchTitle="请输入pk码后6位"
              parentMethod={handleSearch}
              title="扫描结果"></HeaderScreen>
            {detailInfo?.packages_name?  <Center w="100%" pt="100"  >
              <Text bold> {detailInfo?.packages_name}</Text>
              <Text bold> 第<Text color="#e73223">{detailInfo?.package_index}</Text>包</Text>
              <Text bold> 总共{detailInfo?.packages}包</Text>
            </Center>: null}
          </Box>
    </Pressable>
   );
 };
 
 export default ScanDetailScreen;
 